package main

import "fmt"

type Name[T int | string] []T

type stack[T int | string] struct {
	len  int
	data []T
}

type Slice[T int | string] []T

type Map[K int | string, V any] map[K]V

func Max[T int | string | float64 | float32](v1, v2 T) T {
	if v1 > v2 {
		return v1
	}
	return v2
}

func Walk[T any](data []T, action func(v T)) {
	for _, v := range data {
		action(v)
	}
}

type SumSliceTool[T string | int | float32 | float64] []T

func (t SumSliceTool[T]) Sum() T {
	var ans T
	for i := range t {
		ans += t[i]
	}
	return ans
}

func main() {
	a := []int{1, 23, 44, 55, 66, 7, 8, 89, 10}
	x := 10
	v := Filter(a, func(i int) bool {
		return i > x
	})
	fmt.Println(v)
	fmt.Println(cap(v))

	valSlice := Slice[int]{1, 2, 4, 444, 44, 323, 553}
	val2 := Filter(valSlice, func(i int) bool {
		return i < 10
	})
	fmt.Println(val2)
}

// Filter 泛型过滤器
func Filter[T any](data []T, action func(T) bool) []T {
	valid := 0
	for i := range data {
		if action(data[i]) {
			data[valid] = data[i]
			valid++
		}
	}
	return data[0:valid]
}
